package com.cbgolf.oa.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.activity.result.ScoreWriteActivity;
import com.cbgolf.oa.entity.ScoreHistoryDataBean;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lq on 2017/9/11.
 */

public class ScoreSonAdapter extends MyBaseAdapter<ScoreHistoryDataBean> {
    public List<ScoreHistoryDataBean> list;
    Map<Integer, Boolean> state = new HashMap<>();

    public ScoreSonAdapter(int itemId, Context context, List<ScoreHistoryDataBean> list) {
        super(itemId, context, list);
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                state.put(i, true);
            }
        }
    }

    int[] scoreNums = new int[]{
            R.id.item_score_num1_tv,
            R.id.item_score_num2_tv,
            R.id.item_score_num3_tv,
            R.id.item_score_num4_tv
    };

    int[] scoreNames = new int[]{
            R.id.item_score_name1_tv,
            R.id.item_score_name2_tv,
            R.id.item_score_name3_tv,
            R.id.item_score_name4_tv
    };
    int[] scoreIds = new int[]{
            R.id.item_score_id1_tv, R.id.item_score_id2_tv, R.id.item_score_id3_tv, R.id.item_score_id4_tv
    };
    int[] scoreTotals = new int[]{
            R.id.item_score_total1_tv, R.id.item_score_total2_tv, R.id.item_score_total3_tv, R.id.item_score_total4_tv
    };

    @Override
    public View getView(final int position, final View view, ViewGroup parent, final ScoreHistoryDataBean data) {
        setText(R.id.item_score_caddie_name_tv, data.recordName);
        setText(R.id.item_score_teetime_tv, !Util.isNull(data.teeTimeDate) ?
                DateUtil.stampToDate(data.teeTimeDate, DateUtil.formatWithbias) : "");
        final View scoreView = view.findViewById(R.id.item_score_layout_ll);

        setVisible(R.id.item_score_layout_ll, (state.containsKey(position) && null != state.get(position)
                && state.get(position)));
        setText(R.id.item_score_open_tv, (state.containsKey(position) && null != state.get(position) && state.get(position))
                ? "收起" : "展开");
        final TextView openTv = (TextView) view.findViewById(R.id.item_score_open_tv);
        final List<ScoreHistoryDataBean.score> list = data.historys;
//        for (int i = 0; i < scoreNums.length; i++) {
//            setText(scoreNames[i], "");
//            setText(scoreIds[i], "");
//            setText(scoreTotals[i], "");
//            setText(scoreNums[i], "");
//        }
//        if (null != list && list.size() > 0) {
//            for (int i = 0; i < list.size(); i++) {
//                setText(scoreNames[i], Util.isNull(list.get(i).name) ? "" : list.get(i).name);
//                setText(scoreIds[i], Util.isNull(list.get(i).identityName) ? "" : list.get(i).identityName);
//                setText(scoreTotals[i], String.valueOf(list.get(i).totalScore));
//                setText(scoreNums[i], Util.isNull(list.get(i).consumerCard) ? "" : list.get(i).consumerCard);
//            }
//        }
        setScoreList(list);

        final View sonItem = view.findViewById(R.id.item_score_son_layout);
        openTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (scoreView.getVisibility() == View.GONE) {
                    openTv.setText("收起");
                    scoreView.setVisibility(View.VISIBLE);
                    state.put(position, true);
                } else {
                    openTv.setText("展开");
                    scoreView.setVisibility(View.GONE);
                    state.put(position, false);
                }
            }
        });

        sonItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == data.historys || data.historys.size() == 0) {
                    return;
                }
                Intent intent = new Intent(context, ScoreWriteActivity.class);
                intent.putExtra("groupId", data.groupId);
                ArrayList<String> phones = new ArrayList<String>();
                for (int i = 0; i < data.historys.size(); i++) {
                    phones.add(data.historys.get(i).phone);
                }
                intent.putStringArrayListExtra("phones", phones);
                context.startActivity(intent);
                ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);
            }
        });
        return view;
    }

    private void setScoreList(List<ScoreHistoryDataBean.score> list) {
        for (int i = 0; i < scoreNums.length; i++) {
            setText(scoreNames[i], "");
            setText(scoreIds[i], "");
            setText(scoreTotals[i], "");
            setText(scoreNums[i], "");
        }

        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                setText(scoreNames[i], Util.isNull(list.get(i).name) ? "" : list.get(i).name);
                setText(scoreIds[i], Util.isNull(list.get(i).identityName) ? "" : list.get(i).identityName);
                setText(scoreTotals[i], String.valueOf(list.get(i).totalScore));
                setText(scoreNums[i], Util.isNull(list.get(i).consumerCard) ? "" : list.get(i).consumerCard);
            }
        }
    }

}
